<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getTenant">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="45">
      </el-table-column>
      <el-table-column type="index" width="45">
      </el-table-column>
      <el-table-column prop="companyName" label="店铺名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="companyNum" label="电话" width="120" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.state === 0" style="color: cadetblue;">待审核</span>
          <span v-else-if="scope.row.state === 1" style="color: green;">审核通过</span>
          <span v-else-if="scope.row.state === 2" style="color: red;">审核失败</span>
          <span v-else style="color: gray;">无状态</span>
        </template>
      </el-table-column>
      <el-table-column prop="type.name" label="类型" width="100" sortable>
      </el-table-column>
      <el-table-column prop="admin.username" label="店长" width="120" sortable>
      </el-table-column>
      <el-table-column prop="address" label="地址" width="200" sortable>
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" width="200" sortable>
      </el-table-column>
      <el-table-column prop="logo" label="logo" width="120" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :page-sizes="[10, 20, 30, 40, 50]"
          :total="pageInfo.total"
          :current-page="query.page"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog width="500px" title="编辑/新增" :visible.sync="tenantFormVisible" :close-on-click-modal="false">
      <el-form :model="tenantForm" label-width="80px" :rules="tenantFormRules" ref="tenantForm">
        <el-form-item label="租户名称" prop="name">
          <el-input v-model="tenantForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="租户描述" prop="description">
          <!--          <el-input v-model="tenantForm.description" auto-complete="off"></el-input>-->
          <el-input type="textarea" v-model="tenantForm.description" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="tenantFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        name: ''
      },
      listLoading: false,
      pageInfo:{
        total: 0,
        rows: []
      },
      query: {
        page: 1,
        rows: 10,
        keyword: ''
      },

      sels: [],//列表选中列

      tenantFormVisible: false,//编辑界面是否显示
      editLoading: false,
      tenantFormRules: {
        name: [
          { required: true, message: '请输入租户类型名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入租户类型描述', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      tenantForm: {
        id: null,
        name: '',
        description: ''
      },
    }
  },
  methods: {
    // 新增&编辑提交
    editSubmit() {
      this.$refs.tenantForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let para = Object.assign({}, this.tenantForm);
            this.$http.put("/sysmanage/tenant",para)
                .then(result => {
                  result = result.data;
                  if (result.success) {
                    this.editLoading = false;
                    this.$message({message: '提交成功', type: 'success'});
                    this.tenantFormVisible = false;
                    this.getTenant();
                  } else {
                    this.$message({message: '提交失败！'+result.message, type: 'error'});
                  }
                })
                .catch(result => {
                  this.$message({message: '网络异常！'+result.message, type: 'error'});
                })
          });
        }
      });
    },
    // 分页大小
    handleSizeChange(val) {
      this.query.rows = val;
      this.getTenant();
    },
    // 下一页&上一页
    handleCurrentChange(val) {
      this.query.page = val;
      this.getTenant();
    },
    // 获取租户类型分页数据
    getTenant() {
      this.$http.post("/sysmanage/tenant/list",this.query)
          .then(result => {
            result = result.data;
            if (result.success) {// true 后端返回了数据
              this.pageInfo = result.resultObj;
            } else {// 后端处理失败
              this.$message({message: result.message,type: 'error'});
            }
          })
          .catch(result => {
            this.$message({message: "网络错误："+result,type: 'error'});
          })
    },
    // 多选
    selsChange: function (sels) {
      this.sels = sels;
    },
    //删除
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {type: 'warning'})
          .then(() => {
            this.listLoading = true;
            //NProgress.start();
            let para = { id: row.id };
            this.$http.delete("/sysmanage/Tenant/"+para)
                .then(result => {
                  result = result.data;
                  if (result.success) {// true 后端返回了数据
                    this.listLoading = false;
                    //NProgress.done();
                    this.$message({message: '删除成功', type: 'success'});
                    this.getTenant();
                  } else {
                    this.$message({message: result.message,type: 'error'});
                  }
                })
          })
          .catch(result => {
            this.$message({message: "网络错误："+result,type: 'error'});
          });
    },
    //显示编辑界面
    handleEdit(index, row) {
      this.tenantFormVisible = true;
      this.tenantForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd() {
      this.tenantFormVisible = true;
      this.tenantForm = {
        id: null,
        name: '',
        description: ''
      };
    },
    //批量删除
    batchRemove() {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {type: 'warning'})
          .then(() => {
            this.listLoading = true;
            let para = { ids: ids };
            batchRemoveUser(para).then((res) => {
              this.listLoading = false;
              this.$message({message: '删除成功', type: 'success'});
              this.getUsers();
            });
          })
          .catch(() => {
          });
    }
  },
  mounted() {
    this.getTenant();
  }
}

</script>

<style scoped>

</style>